<?php
declare(strict_types=1);

namespace App\Admin\Controllers;

use App\Models\Admin\ImageItem;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class ImageItemController extends AdminController
{
    protected $title = "图片库列表";

    public function grid(): Grid
    {
        $grid = new Grid(new ImageItem());
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like("title", "图片名称");
                $filter->equal('is_show', "上架状态")->radio([
                    1 => '上架',
                    2 => '下架',
                ]);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->between('created_at', "录入时间")->datetime();
            });
        });
        $grid->model()->orderByDesc("id");
        $grid->column("uid", "数据编号")->copyable();
        $grid->column("title", "图片名称");
        $grid->column("user.nickname", "发布用户");
        $grid->column("author.nickname", "创作者");
        $grid->column("orders", "显示权重");
        $grid->column("cover", "图片预览")->lightbox(['width' => 50, 'height' => 50]);
        $grid->column("is_show", "上架状态")->display(function ($is_forbidden) {
            if ($is_forbidden == 2) {
                return "<span style='color:red'>下架</span>";
            }
            if ($is_forbidden == 1) {
                return "<span style='color:green'>上架</span>";
            }
            return "<span style='color:gray'>未知</span>";
        });
        $grid->column("created_at", "录入时间");

        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->disableCreateButton();

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new ImageItem());

        $form->text("title", "图片名称")->rules("max:32")->help("最大32个字符");
        $form->radio("is_show", "上架状态")->options([1 => "上架", 2 => "下架"])->default(1);
        $form->number("orders", "显示权重");
        $form->image("path", "图片地址")->uniqueName()->required();
        $form->textarea("remark", "图片描述")->rules("max:255")->help("最大不超过255个字符");

        return $form;
    }
}
